package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.project.system.domain.UserViolation;
import com.happy.cat.project.system.service.IUserViolationService;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 用户违规Controller
 *
 * @author verne
 * @date 2020-08-13
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/userViolation" )
@Api("用户违规 API")
public class UserViolationController extends BaseController {

    private final IUserViolationService iUserViolationService;

    /**
     * 查询用户违规列表
     */
    @ApiOperation(value = "查询用户违规列表",notes = "查询用户违规列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:userViolation:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserViolation userViolation) {
        startPage();
        LambdaQueryWrapper<UserViolation> lqw = new LambdaQueryWrapper<UserViolation>();
        lqw.eq(userViolation.getUserId()>0, UserViolation::getUserId ,userViolation.getUserId());
        List<UserViolation> list = iUserViolationService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出用户违规列表
     */
    @ApiOperation(value = "导出用户违规列表",notes = "导出用户违规列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userViolation:export')" )
    @Log(title = "用户违规" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(UserViolation userViolation) {
        LambdaQueryWrapper<UserViolation> lqw = new LambdaQueryWrapper<UserViolation>(userViolation);
        List<UserViolation> list = iUserViolationService.list(lqw);
        ExcelUtil<UserViolation> util = new ExcelUtil<UserViolation>(UserViolation. class);
        return util.exportExcel(list, "userViolation" );
    }

    /**
     * 获取用户违规详细信息
     */
    @ApiOperation(value = "获取用户违规详细信息",notes = "获取用户违规详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userViolation:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iUserViolationService.getById(id));
    }

    /**
     * 新增用户违规
     */
    @ApiOperation(value = "新增用户违规",notes = "新增用户违规",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userViolation:add')" )
    @Log(title = "用户违规" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserViolation userViolation) {
        return toAjax(iUserViolationService.save(userViolation) ? 1 : 0);
    }

    /**
     * 修改用户违规
     */
    @ApiOperation(value = "修改用户违规",notes = "修改用户违规",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userViolation:edit')" )
    @Log(title = "用户违规" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserViolation userViolation) {
        return toAjax(iUserViolationService.updateById(userViolation) ? 1 : 0);
    }

    /**
     * 删除用户违规
     */
    @ApiOperation(value = "删除用户违规",notes = "删除用户违规",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:userViolation:remove')" )
    @Log(title = "用户违规" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iUserViolationService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
